Boolean Logic Exercises
Part I​
Write down what the following statements will return. Try to figure this out before putting the commands in the chrome console.
2 == "2";
2 === 2;
10 % 3;
10 % 3 === 1;
true && false;
false || true;
true || false;
Part II​
Answer the following questions about this code block:
(a)​
let isLearning = true;
if(isLearning){
console.log("Keep it up!");
} else {
console.log("Pretty sure you are learning....");
}
- What should the above code console.log?
- Why do we not need to specify
if(isLearning === true)
? Why doesif(isLearning)
work on its own?
(b)​
let firstvariable;
let secondvariable = "";
let thirdvariable = 1;
let secretMessage = "Shh!";
if(firstvariable){
console.log("first");
} else if(firstvariable || secondvariable){
console.log("second");
} else if(firstvariable || thirdvariable){
console.log("third");
} else {
console.log("fourth");
}
- What should the above code console.log?
Why?
- What is the value of
firstvariable
when it is initialized? - Is the value of firstvariable a “truthy” value?
Why?
- Is the value of secondvariable a “truthy” value?
Why?
- Is the value of thirdvariable a “truthy” value?
Why?
Part III​
- Research Math.random here and write an if statement that console.log’s “Over 0.5” if Math.random returns a number greater than 0.5. Otherwise console.log “Under 0.5”.
- What is a falsey value? List all the falsey values in JavaScript.
Solutions​
You can find the code here
When you’re ready, move on to Array Basics